<template>
  <el-dialog
    v-model="dialogFormVisible"
    :close-on-click-modal="false"
    :title="title"
    width="520px"
    @close="close"
  >
    <el-form ref="formRef" label-width="100px" :model="form" :rules="rules">
      <el-form-item label="成员账号：" prop="userid">
        <el-input v-model="form.name" placeholder="请输入成员账号" />
      </el-form-item>
      <el-form-item label="成员名称：" prop="name">
        <el-input v-model="form.name" placeholder="请输入部门名称" />
      </el-form-item>
      <el-form-item label="手机号：" prop="mobile">
        <el-input v-model="form.mobile" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="所属部门：" prop="department">
        <el-tree-select
          v-model="form.department"
          check-on-click-node
          check-strictly
          :data="list"
          :default-expanded-keys="[1]"
          node-key="id"
          :render-after-expand="false"
          show-checkbox
        >
          <template #default="{ data }">
            <span>{{ data.department_name }}</span>
          </template>
        </el-tree-select>
      </el-form-item>
      <el-form-item label="职位：" prop="position">
        <el-input v-model="form.mobile" placeholder="请输入职位" />
      </el-form-item>
      <el-form-item label="性别：" prop="gender">
        <el-radio-group v-model="form.gender">
          <el-radio label="1">男</el-radio>
          <el-radio label="2">女</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { doEditUser } from '@/api/enterpriseWeChat/department'
  export default defineComponent({
    name: 'TableEditUser',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')
      const state = reactive({
        formRef: null,
        treeRef: null,
        form: {
          corp_id: null,
          name: '',
          userid: null,
          mobile: '',
          department: '',
          position: '',
          gender: '',
        },
        rules: {
          name: [
            { required: true, trigger: 'blur', message: '请输入成员名称' },
          ],
        },
        title: '',
        list: [],
        dialogFormVisible: false,
      })

      const showEdit = (corp_id, treeList, row) => {
        state.form.corp_id = corp_id
        state.form.gender = '1'
        state.list = treeList
        if (!row) {
          state.title = '创建成员'
        }
        state.dialogFormVisible = true
      }
      const close = () => {
        state['formRef'].resetFields()
        state.form = {}
        state.dialogFormVisible = false
      }
      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            console.log(state.form)
            const { message } = await doEditUser(state.form)
            $baseMessage(message, 'success', 'vab-hey-message-success')
            emit('fetch-data')
            close()
          }
        })
      }
      return {
        ...toRefs(state),
        showEdit,
        close,
        save,
      }
    },
  })
</script>

<style lang="scss" scoped></style>
